package com.pdstudio.carrecom.ui.activity;

import java.util.ArrayList;
import java.util.List;
import com.pdstudio.carrecom.bean.CarSelectInfo;
import com.pdstudio.carrecom.bean.ResultInfo;
import com.pdstudio.carrecom.common.BaseFragmentActivity;
import com.pdstudio.carrecom.ui.adapter.SelectAdapter;
import com.pdstudio.carrecom.ui.view.SlideMenuView;
import com.pdstudio.carrecom.R;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * 
 * 类: ActivitySelectCommon <br>
 * 描述: 选择页面 <br>
 * 作者: ws <br>
 * 时间: 2015-6-30 下午8:13:12
 */
public class ActivitySelectCommon extends BaseFragmentActivity implements
		OnClickListener {

	private ListView mCityListView;
	private List<CarSelectInfo> mDatas = new ArrayList<>();
	private SelectAdapter mCityAdapter;

	private TextView mTitleTextView;
	private ImageView mBackBtn;
	private ProgressBar mTitleProgressBar;

	SlideMenuView mSlideMenu; // 侧边菜单

	private String mTitle;
	private TextView mProgressTip;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_select_common);

		Intent intent = getIntent();
		mDatas = (List<CarSelectInfo>) intent.getSerializableExtra("datas");
		mTitle = intent.getStringExtra("title");

		initView();
		initData();

	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	private void initView() {
		mTitleTextView = (TextView) findViewById(R.id.title_text_nav);
		mTitleTextView.setText(mTitle);
		mBackBtn = (ImageView) findViewById(R.id.title_back);
		mBackBtn.setOnClickListener(this);
		mTitleProgressBar = (ProgressBar) findViewById(R.id.progress);
		mTitleProgressBar.setVisibility(View.VISIBLE);
		mProgressTip = (TextView) findViewById(R.id.prgress_text);

		mCityListView = (ListView) findViewById(R.id.citys_list);
		mCityListView.setEmptyView(findViewById(R.id.citys_list_empty));
		mCityListView
				.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						// TODO Auto-generated method stub

						startActivity(mCityAdapter.getItem(position));
						finish();
					}
				});
	}

	android.widget.AdapterView.OnItemClickListener _listerItemListener = new android.widget.AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {

		}
	};

	private void startActivity(CarSelectInfo car) {
		// Intent i = new Intent();
		Intent i = getIntent();
		// Bundle bundle = new Bundle();
		// bundle.putSerializable("select", car);
		i.putExtra("select", car);
		setResult(1, i);
		finish();
	}

	private void initData() {
		if (mDatas != null && mDatas.size() > 0) {
			mCityAdapter = new SelectAdapter(ActivitySelectCommon.this, mDatas);
			mCityListView.setAdapter(mCityAdapter);
			mTitleProgressBar.setVisibility(View.GONE);
		} else {
			mTitleProgressBar.setVisibility(View.GONE);
			mProgressTip.setText("对不起，没有数据！");
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (mCityAdapter != null && mCityAdapter.getCount() > 0) {
				startActivity(mCityAdapter.getItem(0));
			} else {
				startActivity(new CarSelectInfo());
			}
			return true;
		}
		return false;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ib_clear_text:
			break;
		case R.id.title_back:
			if (mCityAdapter != null && mCityAdapter.getCount() > 0) {
				startActivity(mCityAdapter.getItem(0));
			} else {
				startActivity(new CarSelectInfo());
			}
			break;
		default:
			break;
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		// 主动释放内存
		try {
			mDatas = null;
			System.gc();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
